package com.example.springboottestgit.controller.main;

import com.alibaba.fastjson.JSONObject;
import com.example.springboottestgit.bean.InstructData;
import com.example.springboottestgit.bean.vo.InputVO;
import com.example.springboottestgit.bean.vo.TextVO;
import com.example.springboottestgit.utils.ExcelExportUtil;
import com.example.springboottestgit.utils.HttpUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: ZZZ
 */
public class D8095 {
    static List<TextVO> list = new ArrayList<>();
    static List<TextVO> other_question_list = new ArrayList<>();
    static int file_count = 0;
    static String BL = "";
    static String file_path = "C:\\Users\\user\\Desktop\\莆田市财政局(1)\\莆田市财政局\\#";
    static Map<String, String> file_path_name_map = new HashMap<>();
    static String text_path = "C:\\Users\\user\\Desktop\\文件\\#\\#.txt";
    static String excel_path = "C:\\Users\\user\\Desktop\\文件\\#\\#.xlsx";

    public static void main(String[] args) throws IOException {
        //filePathAndName(new File("C:\\Users\\user\\Desktop\\莆田市财政局(1)\\莆田市财政局\\"));
        List<TextVO> tzz = new ArrayList<>();

        int n = 0;
        int m = 0;
        readExcel("C:\\Users\\user\\Desktop\\文件\\result.xlsx");
       /* List<TextVO> answerLongList = readExcelList("C:\\Users\\user\\Desktop\\文件\\2005-2023-回答太长处理.xlsx");
        Map<String, String> map = new HashMap<>();
        for (TextVO textVO : answerLongList) {
            map.put(textVO.getFileName(), textVO.getAnswer());
        }
*/

        for (int i = 0; i < list.size(); i++) {
            String answer = list.get(i).getAnswer();
            String fileName = list.get(i).getFileName();
            if (answer.contains("所以") || answer.contains("综上所述")||answer.contains("以上")||answer.contains("综上")||answer.contains("因此")) {
                n++;
                System.out.print(i + 2 + " ");
                System.out.println(fileName);
            }else{
                tzz.add(list.get(i));
            }

            if (answer.length() > 500) {
                m++;
            }


        }

//        List<InstructData> instructData = new ArrayList<>();
//        for (TextVO textVO : list) {
//            InstructData instruct = new InstructData();
//            instruct.setInstruction(textVO.getQuestion());
//            instruct.setInput("");
//            instruct.setOutput(textVO.getAnswer());
//            instructData.add(instruct);
//        }
        System.out.println(n);
        System.out.println(m);
        System.out.println(list.size());
        //FileUtil.txtWriter("C:\\Users\\user\\Desktop\\文件\\莆田市财政局-2005-2023-总.txt", JSONObject.toJSONString(instructData));

        //export(tzz, "C:\\Users\\user\\Desktop\\文件\\2005-2023-没有总结.xlsx");
        //export(list, "C:\\Users\\user\\Desktop\\文件\\莆田市财政局-2005-2023-总.xlsx");


    }


    public static void filePathAndName(File dir) throws IOException {
        boolean found = false;//如果没找到要给出回应
        if (dir != null && dir.isDirectory()) {
            File[] files = dir.listFiles();//用数组遍历文件夹中的文件
            if (files != null && files.length > 0) {
                for (File file : files) {
                    if (file.isFile()) {
                        if (file.getName().equals("test.txt")) {
                            continue;
                        }
                        String fileName = file.getName();
                        String filePath = file.getPath();
                        file_path_name_map.put(fileName, filePath);
                    } else {
                        filePathAndName(file);//递归运算
                    }
                }
            }
        } else {
            System.out.println("当前搜索的位置不是文件夹！");
        }
    }

    public static void qwenapi(String fileName, TextVO textVO) throws IOException {
        file_count++;
        System.out.println("文件总数：" + file_count);

        LocalDateTime s = LocalDateTime.now();
        String question = "请帮我提出一个可以回答的问题";
        System.out.println(fileName);

        InputVO inputVO = new InputVO();
        inputVO.setQuestion(question);

        inputVO.setFileName("/" + BL + "/" + fileName);
        System.out.println(inputVO.getFileName());

        String param = JSONObject.toJSONString(inputVO);

        String result = HttpUtils.post("http://192.168.80.35:8099/qwen2/api", param);

        System.out.println(result);
        //saveQuestion(result);
        saveQuestion(result, fileName, textVO);

        LocalDateTime e = LocalDateTime.now();

        Duration between = Duration.between(s, e);
        System.out.println("耗时：" + between.getSeconds());
        System.out.println();
    }

    public static void saveQuestion(String content, String fileName, TextVO vo) {


        content = content.replace("：", ":").replace("\\n", "").replace("?", "？");
        String[] split = content.split(":");
        int n = content.substring(0, content.length() > 10 ? 10 : 5).indexOf("问题:");
        int m = content.indexOf("？");

        vo.setContent(content);
        if (n == -1 || m == -1) {

        } else {
            try {

                String question = content.substring(n, m + 1).replace("问题:", "").replace(" ", "").replace(":\n", "");
                //String answer = content.substring(m).replace("回答:", "").replace(" ", "");
                System.out.println("问题：");
                System.out.println(question);
                vo.setQuestion(question);

            } catch (Exception e) {
                System.out.println(content);

            }

        }

        System.out.println(BL);


    }

    public static void export(List<TextVO> result, String outputFilePath) {

        List<Map<String, Object>> list = JSONObject.parseObject(JSONObject.toJSONString(result), List.class);

        //表头数组
        String[] lables = new String[]{"文件名", "问题", "回答", "路径"};
        //查询数据对应的属性数组
        String[] fields = new String[]{"fileName", "question", "answer", "filePath"};
        String title = "test.xlsx";
        String headTitle = "指令集";
        //if (result.size() > 1) {
        //title = result.get(0).getName() + "(" + result.get(0).getCode() + ")" + "历史数据.xlsx";
        //headTitle = result.get(0).getName() + "(" + result.get(0).getCode() + ")" + "历史数据";
        //}
        ExcelExportUtil.export(outputFilePath, list, lables, fields, title);

    }

    public static void readExcel(String excelFilePath) {
        try (FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
             Workbook workbook = new XSSFWorkbook(inputStream)) {
            Sheet sheet = workbook.getSheetAt(0); // 读取第一个工作表
            for (Row row : sheet) { // 迭代每一行
                //System.out.print(row.getCell(0) + "\t");
                //System.out.println(row.getCell(1));
                //System.out.println(row.getCell(1));

                String fileName = row.getCell(0) + "";
                String question = row.getCell(1) + "";
                String answer = row.getCell(2) + "";
                TextVO vo = new TextVO();
                vo.setFileName(fileName);
                vo.setAnswer(answer);
                if ("文件名".equals(fileName)) {
                    continue;
                }

                vo.setQuestion(question);
                list.add(vo);
//                for (Cell cell : row) { // 迭代每一列
//                    // 根据不同数据类型，以字符串形式输出数据
//                    System.out.print(cell.getStringCellValue() + "\t");
//
//                }
//                System.out.println(); // 换行
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static List<TextVO> readExcelList(String excelFilePath) {
        List<TextVO> list = new ArrayList<>();
        try (FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
             Workbook workbook = new XSSFWorkbook(inputStream)) {
            Sheet sheet = workbook.getSheetAt(0); // 读取第一个工作表
            for (Row row : sheet) { // 迭代每一行
                //System.out.print(row.getCell(0) + "\t");
                //System.out.println(row.getCell(1));
                //System.out.println(row.getCell(1));

                String fileName = row.getCell(0) + "";
                if ("文件名".equals(fileName)) {
                    continue;
                }
                String question = row.getCell(1) + "";
                String answer = row.getCell(2) + "";
                TextVO vo = new TextVO();
                vo.setFileName(fileName);
                vo.setAnswer(answer);
                vo.setQuestion(question);
                list.add(vo);
//                for (Cell cell : row) { // 迭代每一列
//                    // 根据不同数据类型，以字符串形式输出数据
//                    System.out.print(cell.getStringCellValue() + "\t");
//
//                }
//                System.out.println(); // 换行
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
}
